package io.task.cc.message.socket.spi.server;

import io.task.cc.message.socket.ChannelContext;
import io.task.cc.message.socket.Config;
import io.task.cc.message.socket.Node;
import lombok.Getter;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.channels.AsynchronousSocketChannel;

@Slf4j
@Setter
@Getter
public class ServerChannelContext extends ChannelContext {


    public ServerChannelContext(Config config, AsynchronousSocketChannel asynchronousSocketChannel) {
        super(config, asynchronousSocketChannel);
    }


    @Override
    public Node createClientNode(AsynchronousSocketChannel asynchronousSocketChannel) throws IOException {

        InetSocketAddress inetSocketAddress = (InetSocketAddress) asynchronousSocketChannel.getRemoteAddress();

        Node clientNode = new Node(inetSocketAddress.getHostString(), inetSocketAddress.getPort());

        return clientNode;
    }


    @Override
    public boolean isServer() {
        return true;
    }

}
